<?php
/**
 * 活动中奖纪录
 * http://baseAddress/api/goods_record/
 */
namespace App\Http\Controllers\Api\GoodsRecord;

use App\Http\Controllers\Controller;
use App\Models\GoodsRecord\GoodsRecordService;
use Illuminate\Http\Request;

class GoodsRecordController extends Controller
{
    /**
     * 添加商品使用纪录
     * @param Request $request
     * @param GoodsRecordService $goodsRecordService
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request, GoodsRecordService $goodsRecordService)
    {
        $result = $goodsRecordService->createGoods($request->request->all());
        if ($result) {
            return $this->respondJson(array('record' => $result));
        } else {
            return $this->respondJson(array('record' => array(), 'code' => 1));
        }
    }

    /**
     * 删除商品使用记录
     * @param Request $request
     * @param GoodsRecordService $goodsRecordService
     * @return \Illuminate\Http\JsonResponse
     */
    public function remove(Request $request, GoodsRecordService $goodsRecordService)
    {
        $recordInfo = $request->input('record');
        if (is_string($recordInfo)) {
            $recordInfo = json_decode(str_replace('\'', '"', $recordInfo), true);
        }
        $conditions = $request->input('conditions');
        if (is_string($conditions)) {
            $conditions = json_decode(str_replace('\'', '"', $conditions), true);
        }
        $result = $goodsRecordService->modify($recordInfo, $conditions);
        if ($result) {
            return $this->respondJson(array('record' => $result));
        } else {
            return $this->respondJson(array('record' => array(), 'code' => 1));
        }
    }

    /**
     * 获取商品使用记录详情
     * @param Request $request
     * @param GoodsRecordService $goodsRecordService
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request, GoodsRecordService $goodsRecordService)
    {
        $records = $goodsRecordService->getByGoodsId($request->request->get('goodsId'));
        if ($records) {
            return $this->respondJson(array('records' => $records));
        } else {
            return $this->respondJson(array('records' => null, 'code' => 1));
        }
    }
}
